package cc.rengu.igas.share.biz.service.impl;

import cc.rengu.igas.share.common.enums.RespCodeEnum;
import cc.rengu.igas.share.facade.UpacUnifiedService;
import cc.rengu.igas.share.facade.request.UpacPayResultNoticeRequest;
import cc.rengu.igas.share.facade.request.UpacRefundResultNoticeRequest;
import cc.rengu.igas.share.facade.request.UpacSignResultNoticeRequest;
import cc.rengu.igas.share.facade.response.UpacPayResultNoticeResponse;
import cc.rengu.igas.share.facade.response.UpacRefundResultNoticeResponse;
import cc.rengu.igas.share.facade.response.UpacSignResultNoticeResponse;
import cc.rengu.igas.share.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * 银联全渠道业务统一处理服务
 * Created by wangcheng on 2021/9/29 15:55
 */
public class UpacUnifiedServiceImpl implements UpacUnifiedService {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<UpacPayResultNoticeResponse> payResultNotice(UpacPayResultNoticeRequest request) throws Exception {
        DubboService dubboService = new DubboServiceImpl();
        Result<UpacPayResultNoticeResponse> result = new Result<>();
        UpacPayResultNoticeResponse payResultNoticeResponse = new UpacPayResultNoticeResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, UpacPayResultNoticeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((UpacPayResultNoticeResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联二维码收款通知处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                payResultNoticeResponse.setRespCode(((BizException) e).getCode());
                payResultNoticeResponse.setRespDesc(e.getMessage());
            } else {
                payResultNoticeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                payResultNoticeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(payResultNoticeResponse);
        }
        return result;
    }

    @Override
    public Result<UpacRefundResultNoticeResponse> refundResultNotice(UpacRefundResultNoticeRequest request) throws Exception {
        DubboService dubboService = new DubboServiceImpl();
        Result<UpacRefundResultNoticeResponse> result = new Result<>();
        UpacRefundResultNoticeResponse refundResultNoticeResponse = new UpacRefundResultNoticeResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, UpacRefundResultNoticeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((UpacRefundResultNoticeResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联全渠道退款通知处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                refundResultNoticeResponse.setRespCode(((BizException) e).getCode());
                refundResultNoticeResponse.setRespDesc(e.getMessage());
            } else {
                refundResultNoticeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                refundResultNoticeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(refundResultNoticeResponse);
        }
        return result;
    }

    @Override
    public Result<UpacSignResultNoticeResponse> payOpenNotice(UpacSignResultNoticeRequest request) throws Exception {
        DubboService dubboService = new DubboServiceImpl();
        Result<UpacSignResultNoticeResponse> result = new Result<>();
        UpacSignResultNoticeResponse upacPayOpenNoticeResponse = new UpacSignResultNoticeResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, UpacSignResultNoticeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((UpacSignResultNoticeResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联全渠道签约结果通知处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                upacPayOpenNoticeResponse.setRespCode(((BizException) e).getCode());
                upacPayOpenNoticeResponse.setRespDesc(e.getMessage());
            } else {
                upacPayOpenNoticeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                upacPayOpenNoticeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(upacPayOpenNoticeResponse);
        }
        return result;
    }
}
